import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
import * as path from 'path';

// for config.{env}.ts
export type DefaultConfig = PowerPartial<EggAppConfig & BizConfig>;

// app special config scheme
export interface BizConfig {
    sourceUrl: string;
}


export default (appInfo: EggAppInfo) => {
    const config = {} as PowerPartial<EggAppConfig> & BizConfig;

    // app special config
    config.sourceUrl = `https://github.com/eggjs/examples/tree/master/${appInfo.name}`;

    // override config from framework / plugin
    // use for cookie sign key, should change to your own and keep security
    config.keys = appInfo.name + '_1534490368803_557';

    // add your config here
    config.middleware = ['checkToken'];
    config.checkToken = {
        name: 'cbl',
    };

    config.development = {
        watchDirs: [],
        ignoreDirs: [],
        fastReady: false,
        reloadOnDebug: true,
        overrideDefault: false,
        reloadPattern: undefined,
    };
    config.sequelize = {
        dialect: 'mysql',
        database: 'db_official',
        host: '127.0.0.1',
        port: 3306,
        username: 'root',
        password: 'chenbailin',
        pool: {
            max: 100,
            min: 10,
        },
        dialectOptions: {
            charset: 'utf8mb4'
        },
    };
    config.cors = {
        origin: '*',
        allowMethods: 'GET,POST,PUT,DELETE,OPTIONS',
    };
    config.multipart = {
        whitelist: [
            '.png',
            '.jpg',
            '.jpeg',
            '.gif',
        ],
        requireFile: false,
    };
    config.static = {
        prefix: '/',
        dir: path.join(appInfo.baseDir, 'app/public/img'),
        dynamic: true,
        preload: false,
        maxAge: 31536000
    };
    config.onerror = {
        json: (err, ctx) => {
            let result: any;
            try {
                result = JSON.parse(err.message);
            } catch (e) {
                result = err.message;
            }
            if (result.code) {
                ctx.body = result;
            } else {
                ctx.body = { code: 3, msg: result };
            }
            ctx.status = 200;
        },
        html: (err, ctx) => {
            let result: any;
            try {
                result = JSON.parse(err.message);
            } catch (e) {
                result = err.message;
            }
            if (result.code) {
                ctx.body = JSON.stringify(result);
            } else {
                ctx.body = JSON.stringify({ code: 3, msg: result });
            }
            ctx.status = 200;
        },
    };
    config.security = {
        csrf: false,
    };
    config.weixin = {
        appId: 'c8a72cbd7fa9d8f682cb033be73cac2b',
        appSecret: 'f839f7c17664371b9fa02180ce036e1b'
    };
    config.SMS = {
        AccessKeyID: 'LTAIiL6S23ytIo8H',
        AccessKeySecret: 'j3oLUqgqYyopeDmGOdciN3sONEqyrK'
    };
    config.baseUrl = 'http://hx.frp.blueleader.top/';
    config.redirectUrl = 'http://jywechat.frp.blueleader.top/';
    config.logger = {
        dir: './logs/example'
    }
    config.io = {
        namespace: {
            '/': {
                connectionMiddleware: ['auth'],
                packetMiddleware: ['filter'],
            }
        },
    }
    config.redis={
        client:{
            port:6379,
            host:'127.0.0.1',
            password:'',
            db:0
        }
    }
    return config;
};
